package com.alan.demo.utilt.sql;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class SqlUtils {

    private static boolean open = false;
    private static List<String> sqlList = new ArrayList<>();

    private SqlUtils() {
    }

    public static void open() {
        synchronized (SqlUtils.class) {
            open = true;
        }
    }

    public static boolean isOpen() {
        return open;
    }

    public static List<String> getSqlList() {

        synchronized (SqlUtils.class) {
            List<String> result = new ArrayList<>(sqlList);
            sqlList = new ArrayList<>();
            return result;
        }

    }

    public static void addSql(String sql) {

        synchronized (SqlUtils.class) {
            sqlList.add(sql + ";");
            open = false;
        }

    }

    public static <T> T open(Callable<T> loader) {
        synchronized (SqlUtils.class) {
            open = true;
            try {
                return loader.call();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }


}
